package gxprod.androidai.functions;

import gxprod.androidai.AIActivity;
import gxprod.androidai.BubbleAdapter;
import gxprod.androidai.DataCollection;
import gxprod.androidai.FunctionManager;
import gxprod.androidai.regex.MatchResult;
import gxprod.androidai.regex.NamedPattern;
import android.util.Log;


/**
 * Interactions between the user and the ai, such as saying hello
 * @author Josh Maleszewski
 */
public class InteractionFunction extends Function {

	private boolean hasToldUserMyName = false;

	private NamedPattern[] patterns = new NamedPattern[] {
			// 0
			NamedPattern.compile("^(hi|hello|hola)"),
			NamedPattern.compile("^((good)?bye|see y(a|ou)( later)?|later|astalavista|so long)( eve)?$"),
			NamedPattern.compile("how are you( doing)?"),
			NamedPattern.compile("(whats up|sup|que pasa)"),
			NamedPattern.compile("^(im( doing)? )?(fine|good|great|well|terrific|happy)"),
			// 5
			NamedPattern.compile("^(im (doing )?(good$|fine|great|happy)|^(bad|horrible|tired|upset|sad|))$"),
			NamedPattern.compile("^(my name is|i am|im) (([a-zA-Z]+){1,2})"),
			NamedPattern.compile("what( i)?s (my|your) name"),
			NamedPattern.compile("call me (([a-zA-Z]+){1,2})"),
			NamedPattern.compile("i (have|need) to (find|locate) ([\\w ]+)"),
			// 10
			NamedPattern.compile("(that (didnt|wont) work|i (already )?tried that)"),
	};

	@Override
	public MatchResult isMatch(String statement) {
		return super.isMatch(statement, patterns, 0.45f);
	}

	@Override
	public boolean execute(MatchResult result, BubbleAdapter adapter) {
		String out, name;
		switch(result.MatchIndex) {

			case 0:		// hi
				if(DataCollection.User.containsKey(DataCollection.USER_NICNAME))
					adapter.output("Hi, " + DataCollection.User.get(DataCollection.USER_NICNAME));
				else if(DataCollection.User.containsKey(DataCollection.USER_NAME))
					adapter.output("Hello " + DataCollection.User.get(DataCollection.USER_NAME));
				else
					adapter.output("Hello");
				break;

			case 1: 	// goodbye
				out = "Goodbye";
				if(DataCollection.User.containsKey(DataCollection.USER_NICNAME))
					out += ", " + DataCollection.User.get(DataCollection.USER_NICNAME);
				adapter.say(out);
				AIActivity.get().finish();				
				break;

			case 2: adapter.output("I'm doing good. How about you?"); break;
			case 3: adapter.output("Not much"); break;
			case 4:	adapter.output("That's good."); break;
			case 5: adapter.output("That's not good."); break;

			case 6:		// my name is *
				name = result.Matcher.group(2);
				name = capitalizeString(name);

				out = "It's nice to meet you " + name + '.';
				if(!hasToldUserMyName) 
					out += " My name is Eve";

				adapter.output(out);				
				DataCollection.User.put(DataCollection.USER_NAME, name);
				break;

			case 7:
				if(FunctionManager.getRawInput().contains("my")) {
					if(DataCollection.User.containsKey(DataCollection.USER_NAME))
						adapter.output("You're name is " + DataCollection.User.get(DataCollection.USER_NAME));
					else
						adapter.output("I don't know you're name");
				} else {
					hasToldUserMyName = true;
					out = "I'm Eve.";
					if(DataCollection.User.get(DataCollection.USER_NAME) == null) out += " What is you're name";
					adapter.output(out);
				}
				break;

			case 8:
				name = result.Matcher.group(2);
				adapter.output("Ok, from now on i will call you " + name);
				DataCollection.User.put(DataCollection.USER_NICNAME, name);
				break;

			case 9:
				adapter.output("Try retracing your steps");
				break;
				
			case 10:
				adapter.output("Then I'm all out of ideas");
				break;
				
			case 11:
				
				
			default :
				Log.e(AIActivity.TAG, "invalid match result index: " + result.MatchIndex + " in InteractionFunction");
				return false;
		}
		return true;
	}

	@Override
	public boolean cullFunction(String statement) { return true; }

	public static String capitalizeString(String string) {
		char[] chars = string.toLowerCase().toCharArray();
		boolean found = false;
		for (int i = 0; i < chars.length; i++) {
			if (!found && Character.isLetter(chars[i])) {
				chars[i] = Character.toUpperCase(chars[i]);
				found = true;
			} else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here
				found = false;
			}
		}
		return String.valueOf(chars);
	}
}
